package com.woniuxy.happylife.suggest.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.happylife.entity.ComplaintType;
import com.woniuxy.happylife.entity.Complaints;
import com.woniuxy.happylife.entity.ComplaintsDTO;
import com.woniuxy.happylife.entity.ResponseEntity;
import com.woniuxy.happylife.suggest.service.ComplaintTypeService;
import com.woniuxy.happylife.suggest.service.ComplaintsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author Fu
 * @Date 2022-02-19 13:18
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("complaints")
public class ComplaintsController {
    @Resource
    ComplaintsService complaintsService;
    @Resource
    ComplaintTypeService complaintTypeService;

    @PostMapping("insert")
    public ResponseEntity insert(@RequestParam("file") MultipartFile file,Complaints complaints){
        int i = complaintsService.insert(file,complaints);
        if(i>0){
            return new ResponseEntity(200,"成功",null);
        }
        return new ResponseEntity(400,"失败",null);
    }
    @GetMapping("findAll/{page}/{limit}/{businessId}")
    public ResponseEntity findByCondition(
            @PathVariable("page")int page,
            @PathVariable("limit") int limit, @PathVariable("businessId") int businessId){
        PageHelper.startPage(page,limit);
        List<Complaints> list = complaintsService.findAll(businessId);
        if(list.size()!=0){
            PageInfo<Complaints> pageInfo = new PageInfo<>(list);
            return new ResponseEntity(200,"成功",pageInfo);
        }
        return  new ResponseEntity(400,"没有查到",null);
    }
    @PostMapping("update")
    public ResponseEntity update(@RequestBody Complaints complaints){
        complaints.setHandleTime(new Date());
        log.info(complaints.toString());
        int i = complaintsService.update(complaints);
        if(i>0){
            return new ResponseEntity(200,"成功",null);
        }
        return new ResponseEntity(400,"失败",null);
    }
    @GetMapping("findByCid/{complaintsId}")
    public ResponseEntity findByCid(@PathVariable("complaintsId")long complaintsId){
        ComplaintsDTO complaints =complaintsService.findByCid(complaintsId);
        log.info(complaints.toString());
        if(complaints !=null){
            return new ResponseEntity(200,"成功",complaints);
        }
        return new ResponseEntity(400,"失败",null);
    }
    @GetMapping("deleteByComplaintsId/{complaints_id}")
    public ResponseEntity deleteByComplaintsId(@PathVariable("complaints_id") int complaints_id){
       int result =  complaintsService.deleteByComplaintsId(complaints_id);
       if(result>0){
           return new ResponseEntity(200,"删除成功",null);
       }
       return new ResponseEntity(400,"删除失败",null);
    }
    @PostMapping("findByCondition/{page}/{limit}")
    public ResponseEntity findByCondition(@PathVariable("page") int page ,@PathVariable("limit") int limit,
                                          @RequestBody Complaints complaints){

        PageHelper.startPage(page,limit);

        List<Complaints> list = complaintsService.findByCondition(complaints);
        PageInfo<Complaints> pageInfo = new PageInfo<>(list);
        if(list.size()>0){

            return new ResponseEntity(200,"查询成功",pageInfo);
        }

        return new ResponseEntity(400,"查询失败",pageInfo);
    }
    @GetMapping("findComplaintType")
    public ResponseEntity findComplaintType(){
        List<ComplaintType> list =  complaintTypeService.findComplaintType();
        return new ResponseEntity(200,"查询成功",list);

    }
}
